package zoo.member.admin.pro.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;



import zoo.member.admin.pro.dao.AdminProductDao;
import zoo.member.admin.pro.domain.AdminProductCommand;
import zoo.member.admin.util.FileUtil;

@Controller
@RequestMapping("/admin/modify.do")
public class AdminProductModifyController {

	@Autowired
	AdminProductDao apDao;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView form(@RequestParam("p_code")int p_code){
		AdminProductCommand proCommand = apDao.selectProBoard(p_code);
		return new ModelAndView("promodify","proCommand",proCommand);
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String submit(@ModelAttribute("command")AdminProductCommand proCommand,MultipartHttpServletRequest request
			) throws Exception{
		
					
		AdminProductCommand proCommand2 = null;
		String oldFileName ="";
		proCommand2 = apDao.selectProBoard(proCommand.getP_code());
		List<MultipartFile> list = request.getFiles("reportFile");
			
	/*		if(!proCommand2.getPwd().equals(boardCommand.getPwd())){
				result.rejectValue("pwd", "invalidPassword");
				return formViewName;
			}else{
				//기존 파일명을 구함
				//업로드되는 파일이 있을 경우 기존 파일을 삭제 새로운 파일명 셋팅
				//업로드되는 파일이 없을 경우 기존 파일명을 셋팅*/
				
				
				if(list != null && list.size()>0){
					for(int i=0;i<list.size();i++){
						MultipartFile m = (MultipartFile)list.get(i);
						if(m !=null && !m.isEmpty()){
							
						oldFileName = m.getOriginalFilename();
						
						
					//전송되어온 파일이 있을 경우
						if(m !=null && !m.isEmpty()){
							
							String FileName = FileUtil.rename(m.getOriginalFilename());
							
							if(i == 0) proCommand.setP_listimage(FileName);
							else if(i == 1) proCommand.setP_previewimage1(FileName);
							else if(i == 2) proCommand.setP_previewimage2(FileName);
							else if(i == 3) proCommand.setP_previewimage3(FileName);
							else if(i == 4) proCommand.setP_productimage1(FileName);
							else if(i == 5) proCommand.setP_productimage2(FileName);
							else if(i == 6) proCommand.setP_productimage3(FileName);
							
				    try {
						File file = new File(FileUtil.UPLOAD_PATH + "/" + FileName);
						m.transferTo(file);
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
						}
				else{
									
					if(i == 0) proCommand.setP_listimage(oldFileName);
					else if(i == 1) proCommand.setP_previewimage1(oldFileName);
					else if(i == 2) proCommand.setP_previewimage2(oldFileName);
					else if(i == 3) proCommand.setP_previewimage3(oldFileName);
					else if(i == 4) proCommand.setP_productimage1(oldFileName);
					else if(i == 5) proCommand.setP_productimage2(oldFileName);
					else if(i == 6) proCommand.setP_productimage3(oldFileName);
				}}
	
					if(oldFileName != null){
						//이전 파일 삭제
					FileUtil.removeFile(oldFileName);
					}
				}
				apDao.update(proCommand);
			}
		
		return "redirect:/admin/prolist.do";
	}	
}
